import { mapGetters } from 'vuex'
import { MANAGE_JOBS } from '@/credit/plugins/constant'
import { exportExcel } from '@/credit/mixin/taskTableProvinceUnion'

export default {
  data() {
    return {
      loading: false,
      tableData: [],
      totalCount: 0,
      exportLoading: false,
      useManage: true,
      dateFmt: 'yyyy-MM-dd hh:mm',
    }
  },
  computed: {
    ...mapGetters(['clientId', 'job', 'userId']),
    manageParams() {
      const params =
        this.useManage && MANAGE_JOBS.includes(this.job)
          ? {
              'service_id.eq': this.clientId,
              // 'orWhere.recommend_service.eq': this.clientId
            }
          : {
              'service_id.eq': this.clientId,
              'orWhere.res_a.eq': this.userId,
              'orWhere.res_b.eq': this.userId,
              'orWhere.pre_approval_person.eq': this.userId,
            }
      return params
    },
    tableName() {
      return this.useRegisterTable ? 'customer_related' : 'customer_related'
    },
  },
  methods: {
    getParams() {
      return {
        ...this.params,
        ...this.manageParams,
      }
    },
    fetchData(params) {
      return this.$utils.promise(
        this.$api.getData(this.$serviceName, this.tableName, params, this.order || {})
      )
    },
    async getData(index = 1) {
      this.params.index = index
      this.loading = true
      const [res = {}] = await this.fetchData(this.getParams())
      this.loading = false

      const data = res.data || {}
      this.totalCount = data.totalCount || 0
      this.tableData = (data.data || []).map(this.dataAdapter)
    },
    async exportExcel(filename) {
      this.exportLoading = true
      exportExcel({
        filename,
        tableColumns: this.tableColumns,
        params: this.getParams(),
        dataAdapter: this.dataAdapter,
        fetchData: this.fetchData,
        onLoaded: () => {
          this.exportLoading = false
        },
      })
    },
  },
}
